﻿using System.Collections.Generic;

namespace Development.Tools
{
    public class SafeContainer<T>
    {
        protected SortedDictionary<long, T> m_objects = new SortedDictionary<long, T>();

        public bool Add(long id, T val)
        {
            lock (m_objects)
            {

                if (m_objects.ContainsKey(id))
                {
                    return false;
                }
                m_objects.Add(id, val);
            }

            return true;
        }

        public void Remove(long id)
        {
            lock (m_objects)
            {
                m_objects.Remove(id);
            }
        }

        public T Get(long id)
        {
            lock (m_objects)
            {
                if (m_objects.ContainsKey(id))
                {
                    return m_objects[id];
                }
            }
            return default(T);
        }
        public void RemoveAll()
        {
            lock (m_objects)
            {
                m_objects.Clear();
            }
        }
        public int GetCount()
        {
            lock (m_objects)
            {
                return m_objects.Count;
            }

        }

        public List<T> GetObjectList()
        {
            lock (m_objects)
            {
                List<T> list = new List<T>(m_objects.Values);
                return list;
            }
        }
        public virtual void Loop(long currTime)
        {

        }



    }
}
